package com.ruoyi.fire.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.ResponseEntityInfo;
import com.ruoyi.common.enums.RequestMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.RequestService;
import com.ruoyi.fire.domain.EEconomicType;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.fire.mapper.EBuildingFireHazardMapper;
import com.ruoyi.fire.domain.EBuildingFireHazard;
import com.ruoyi.fire.service.IEBuildingFireHazardService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 建筑物火灾危险性Service业务层处理
 *
 * @author ruoyi
 * @date 2025-11-02
 */
@Service
@RequiredArgsConstructor
public class EBuildingFireHazardServiceImpl extends ServiceImpl<EBuildingFireHazardMapper, EBuildingFireHazard> implements IEBuildingFireHazardService {
    @Autowired
    private EBuildingFireHazardMapper eBuildingFireHazardMapper;

    private final RequestService requestService;

    /**
     * 查询建筑物火灾危险性
     *
     * @param id 建筑物火灾危险性主键
     * @return 建筑物火灾危险性
     */
    @Override
    public EBuildingFireHazard selectEBuildingFireHazardById(Long id) {
        return eBuildingFireHazardMapper.selectEBuildingFireHazardById(id);
    }

    /**
     * 查询建筑物火灾危险性列表
     *
     * @param eBuildingFireHazard 建筑物火灾危险性
     * @return 建筑物火灾危险性
     */
    @Override
    public List<EBuildingFireHazard> selectEBuildingFireHazardList(EBuildingFireHazard eBuildingFireHazard) {
        return eBuildingFireHazardMapper.selectEBuildingFireHazardList(eBuildingFireHazard);
    }

    /**
     * 新增建筑物火灾危险性
     *
     * @param eBuildingFireHazard 建筑物火灾危险性
     * @return 结果
     */
    @Override
    public int insertEBuildingFireHazard(EBuildingFireHazard eBuildingFireHazard) {
        return eBuildingFireHazardMapper.insertEBuildingFireHazard(eBuildingFireHazard);
    }

    /**
     * 修改建筑物火灾危险性
     *
     * @param eBuildingFireHazard 建筑物火灾危险性
     * @return 结果
     */
    @Override
    public int updateEBuildingFireHazard(EBuildingFireHazard eBuildingFireHazard) {
        eBuildingFireHazard.setUpdateTime(DateUtils.getNowDate());
        return eBuildingFireHazardMapper.updateEBuildingFireHazard(eBuildingFireHazard);
    }

    /**
     * 批量删除建筑物火灾危险性
     *
     * @param ids 需要删除的建筑物火灾危险性主键
     * @return 结果
     */
    @Override
    public int deleteEBuildingFireHazardByIds(Long[] ids) {
        return eBuildingFireHazardMapper.deleteEBuildingFireHazardByIds(ids);
    }

    /**
     * 删除建筑物火灾危险性信息
     *
     * @param id 建筑物火灾危险性主键
     * @return 结果
     */
    @Override
    public int deleteEBuildingFireHazardById(Long id) {
        return eBuildingFireHazardMapper.deleteEBuildingFireHazardById(id);
    }

    /**
     * 数据同步
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dataSync() {
        try {
            ResponseEntityInfo responseEntityInfo = requestService.advancedRequest(RequestMapper.BUILDING_FIRE_HAZARD, null);
            if (responseEntityInfo != null && responseEntityInfo.getErrno() == 0) {
                Object data = responseEntityInfo.getData();
                if (data == null) {
                    // 记录警告日志：返回数据为空
                    return;
                }

                // 使用条件删除替代全表删除，绕过保护机制
                getBaseMapper().delete(new LambdaQueryWrapper<EBuildingFireHazard>().isNotNull(EBuildingFireHazard::getId));
                List<EBuildingFireHazard> eEconomicTypes = JSON.parseArray(JSON.toJSONString(data), EBuildingFireHazard.class);
                if (eEconomicTypes != null && !eEconomicTypes.isEmpty()) {
                    // 设置更新时间
                    eEconomicTypes.forEach(item -> item.setUpdateTime(DateUtils.getNowDate()));
                    saveBatch(eEconomicTypes);
                }
            }
        } catch (Exception e) {
            // 记录错误日志
            throw new RuntimeException("数据同步过程中发生异常", e);
        }
    }
}
